package num25;

/**
 * @author : DuJiabao
 * @Date : 2021/1/25 11:19
 * @Project : sword-finger-offer
 * @File : null.java
 * @Desc :输入两个递增排序的链表，合并这两个链表并使新链表中的节点仍然是递增排序的。
 * https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof/
 * 方法：新建一个链表，然后逐个结点接。会比较简单。
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
//        伪头节点
        ListNode node = new ListNode(0);
        ListNode newHead = node;  // 头指针
        while(l1 != null && l2 != null){
            if(l1.val < l2.val){
                newHead.next = l1;
                l1 = l1.next;
            }else{
                newHead.next = l2;
                l2 = l2.next;
            }
            newHead = newHead.next;
        }
        if(l1 != null){
            newHead.next = l1;
        }
        if(l2 != null){
            newHead.next = l2;
        }
        return node.next;
    }
}
